//=============================================================================
// ProMesh.NET - .NET Web Application Framework 
//
// Copyright (c) 2003-2008 Philippe Leybaert
//
// Permission is hereby granted, free of charge, to any person obtaining a copy 
// of this software and associated documentation files (the "Software"), to deal 
// in the Software without restriction, including without limitation the rights 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
// copies of the Software, and to permit persons to whom the Software is 
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in 
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//=============================================================================

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.Caching;

namespace Activa.ProMesh
{
	public static class WebAppContext
	{
	    private static readonly ClientDataCollection _getParameters = new ClientDataCollection(false);
        private static readonly ClientDataCollection _postParameters = new ClientDataCollection(true);

	    public static SessionBase          Session     { get { return ProMeshHttpContext.Current.SessionObject; } }
        public static ProMeshHttpContext   HttpContext { get { return ProMeshHttpContext.Current;               } }
		public static ProMeshHttpResponse  Response    { get { return ProMeshHttpContext.Current.Response;      } }
		public static IHttpRequest         Request     { get { return ProMeshHttpContext.Current.Request;       } }
		public static IHttpServerUtility   Server      { get { return ProMeshHttpContext.Current.Server;        } }
		public static Cache                WebCache    { get { return ProMeshHttpContext.Current.Cache;         } }
        public static ClientDataCollection GetData     { get { return _getParameters;                           } }
        public static ClientDataCollection PostData    { get { return _postParameters;                          } }

//        [Obsolete]
//	    public static Controller CurrentPageController
//	    {
//            get { return CurrentController;  }
//	    }
//
//	    public static Controller CurrentController
//	    {
//	        get { return (Controller) ProMeshHttpContext.Current.Items["CurrentPageController"]; }
//            internal set { ProMeshHttpContext.Current.Items["CurrentPageController"] = value; }
//	    }

        public static View RootView
        {
            get { return (View)ProMeshHttpContext.Current.Items["RootView"]; }
            internal set { ProMeshHttpContext.Current.Items["RootView"] = value; }
        }

	    public static AjaxContext AjaxContext
	    {
            get { return (AjaxContext) ProMeshHttpContext.Current.Items["AjaxContext"]; }
            internal set { ProMeshHttpContext.Current.Items["AjaxContext"] = value; }
	    }

        internal static NameValueCollection QueryParameters
	    {
	        get
	        {
	            NameValueCollection value =  (NameValueCollection) ProMeshHttpContext.Current.Items["GetParameters"];

                return value ?? Request.QueryString;
	        }
            set
            {
                ProMeshHttpContext.Current.Items["GetParameters"] = value;
            }
	    }

        internal static void AddControllerParameters(NameValueCollection parameters)
        {
            NameValueCollection newParameters = new NameValueCollection(QueryParameters);

            newParameters.Add(parameters);

            QueryParameters = newParameters;
        }

	    public static bool Offline
	    {
            get { return (ProMeshHttpContext.Current is OfflineHttpContext); }
	    }

        internal static void Reset()
        {
        }

        /// <summary>
        /// Returns the URL without parameters and without language path
        /// </summary>
	    public static string Url
	    {
	        get
	        {
                return UrlHelper.GetUrlPath(Request.AppRelativeCurrentExecutionFilePath, Request.PathInfo);
            }
	    }

        /// <summary>
        /// Returns the controller and action (no extension)
        /// </summary>
        internal static string UrlWithoutExtension
        {
            get
            {
                return UrlHelper.GetUrlPathWithoutExtension(Request.AppRelativeCurrentExecutionFilePath, Request.PathInfo);
            }
        }

        public static bool IsPost()
        {
            return Request.RequestType == "POST";
        }

        public static bool IsPost(string buttonName)
        {
            return IsPost() && (!string.IsNullOrEmpty(PostData[buttonName]));
        }
	}
}
